package cn.itsource.basic.service.impl;

import cn.itsource.basic.Domain.BaseDomain;
import cn.itsource.basic.query.BaseQuery;
import cn.itsource.basic.query.PageList;
import cn.itsource.basic.service.IBaseService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;


public class BaseServiceImpl <T extends BaseDomain,Q extends BaseQuery> implements IBaseService<T,Q> {

    @Autowired
    private Mapper<T> mapper;

    @Override
    public PageList<T> getPageList(Q query) {
        //1.设置分页参数
        PageHelper.startPage(query.getPageNum(),query.getPageSize());
        //2.执行查询操作
        List<T> list = mapper.selectAll();
        //3.封装分页数据
        PageInfo<T> PageInfo = new PageInfo<>(list);
        //4.使用自己的工具类返回分页数据
        PageList<T> PageList = new PageList<>(PageInfo.getTotal(),PageInfo.getList());
        //5.返回数据
        return PageList;
    }

    @Override
    public void addOrUpdate(T t) {
        if(t.getId()==null){
            //没有id,添加
            mapper.insertSelective(t);
        }else {
            //有id,修改
            mapper.updateByPrimaryKeySelective(t);
        }
    }

    @Override
    public void delete(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<T> getAll() {
        return mapper.selectAll();
    }
}
